package com.rock.securityresearch.common

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import scala.util.Try

extension (date: LocalDate) {
    def formatWith(format: String = "yyyyMMdd"): String = {
        val formatter = DateTimeFormatter.ofPattern(format)
        formatter.format(date)
    }
}

extension (dateStr: String) {
    def toDate(format: String = "yyyyMMdd", format2: String = "yyyy-MM-dd"): LocalDate = {
        val t = Try {
            LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(format))
        }
        if(t.isFailure) then LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(format2)) else t.get
    }
}